package show;

import javax.swing.*;
import java.util.*;
import java.sql.*;
import control.CConnection;

public class SantanCharts extends JFrame {
  private static final long serialVersionUID = 7427788351788951077L;
  private boolean cache[] = new boolean[3];
  private CConnection cn = new CConnection();
  
  public SantanCharts() {
    super("SantanCharts");
    initComponents();
    
    this.setLocationRelativeTo(null);
    for (int i = 0; i < 3; i++) cache[i] = false;
  }

  private void initComponents() {
    tpMain = new javax.swing.JTabbedPane();
    pnlCountry = new javax.swing.JPanel();
    pnlTop = new javax.swing.JPanel();
    pnlTotal = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    pnlCountry.addComponentListener(new java.awt.event.ComponentAdapter() {
      public void componentShown(java.awt.event.ComponentEvent evt) {
        pnlCountryComponentShown(evt);
      }
    });
        
    pnlTop.addComponentListener(new java.awt.event.ComponentAdapter() {
      public void componentShown(java.awt.event.ComponentEvent evt) {
        pnlTopComponentShown(evt);
      }
    });
        
    pnlTotal.addComponentListener(new java.awt.event.ComponentAdapter() {
      public void componentShown(java.awt.event.ComponentEvent evt) {
        pnlTotalComponentShown(evt);
      }
    });

    javax.swing.GroupLayout pnlCountryLayout = new javax.swing.GroupLayout(pnlCountry);
    pnlCountry.setLayout(pnlCountryLayout);
    pnlCountryLayout.setHorizontalGroup(
      pnlCountryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 550, Short.MAX_VALUE)
    );
    pnlCountryLayout.setVerticalGroup(
      pnlCountryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 332, Short.MAX_VALUE)
    );

    tpMain.addTab("Biologists - Contributions / Country", pnlCountry);

    javax.swing.GroupLayout pnlTopLayout = new javax.swing.GroupLayout(pnlTop);
    pnlTop.setLayout(pnlTopLayout);
    pnlTopLayout.setHorizontalGroup(
      pnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 550, Short.MAX_VALUE)
    );
    pnlTopLayout.setVerticalGroup(
      pnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 332, Short.MAX_VALUE)
    );

    tpMain.addTab("Biologists - Top10", pnlTop);

    javax.swing.GroupLayout pnlTotalLayout = new javax.swing.GroupLayout(pnlTotal);
    pnlTotal.setLayout(pnlTotalLayout);
    pnlTotalLayout.setHorizontalGroup(
      pnlTotalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 550, Short.MAX_VALUE)
    );
    pnlTotalLayout.setVerticalGroup(
      pnlTotalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 332, Short.MAX_VALUE)
    );

    tpMain.addTab("Members - Per Year", pnlTotal);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(tpMain, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(tpMain, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
    );
        
    this.setResizable(false);
        
    pack();
  }

  private void pnlCountryComponentShown(java.awt.event.ComponentEvent evt) {
    if (!cache[0]) {
      ArrayList<ChartInfo> ci = new ArrayList<ChartInfo>();

      try {
        ResultSet rs = cn.getStatement().executeQuery("SELECT Country, SUM(Contributions) AS Total FROM Biologist GROUP BY Country");
        
        while (rs.next())
          ci.add(new ChartInfo(rs.getInt("Total"), rs.getString("Country")));
        
        GoogleChartApi api = new GoogleChartApi(ci, this.pnlCountry, "Contributions / Country", GoogleChartApi.PIECHART, this.tpMain);
        api.start();
      }
      catch(Exception e) { System.err.println(e.getMessage()); }
      
      cache[0] = true;
    }
  }
  
  private void pnlTopComponentShown(java.awt.event.ComponentEvent evt) {
	if (!cache[1]) {
	  ArrayList<ChartInfo> ci = new ArrayList<ChartInfo>();
      
	  try {
	    ResultSet rs = cn.getStatement().executeQuery("SELECT Passport_Number, Name, Contributions FROM Biologist ORDER BY Contributions DESC");
        
        for (int i = 0; rs.next() && i < 10; i++) {
          char[] label = rs.getString("Name").substring(0, rs.getString("Name").indexOf(' ')).toLowerCase().toCharArray();
          
          label[0] = (char)((int)label[0] - 32);
          
          ci.add(new ChartInfo(rs.getInt("Contributions"), String.valueOf(label)));
        }
        
        GoogleChartApi api = new GoogleChartApi(ci, this.pnlTop, "Top 10 Biologists", GoogleChartApi.BARCHART, this.tpMain);
        api.start();
      }
      catch(Exception e) { System.err.println(e.getMessage()); }
	
      cache[1] = true;
	}
  }
  
  private void pnlTotalComponentShown(java.awt.event.ComponentEvent evt) {
    if (!cache[2]) {
	  ArrayList<ChartInfo> ci = new ArrayList<ChartInfo>();

      try {
        ResultSet rs = cn.getStatement().executeQuery("SELECT year(Joined_Year) as Ano, COUNT(*) AS Membros FROM Member GROUP BY Joined_Year");
        
        while (rs.next())
          ci.add(new ChartInfo(rs.getInt("Membros"), rs.getString("Ano")));
        
        GoogleChartApi api = new GoogleChartApi(ci, this.pnlTotal, "Members / Year", GoogleChartApi.LINECHART, this.tpMain);
        api.start();
      }
      catch(Exception e) { System.err.println(e.getMessage()); }
      
      cache[2] = true;
    }	
  }
  
  public static void main(String args[]) {
    try {
      javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } 
    catch (Exception e) { System.exit(0); }
    
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new SantanCharts().setVisible(true);
      }
    });
  }
   
  private javax.swing.JPanel pnlCountry;
  private javax.swing.JPanel pnlTop;
  private javax.swing.JPanel pnlTotal;
  private javax.swing.JTabbedPane tpMain;
}
